﻿
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.LawElementModel;
using System.Collections.Generic;
using System.Linq;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.BaseModule
{
    /// <summary>
    /// 法律法规基础类  
    /// 情形情节信息中的法律法规与当庭宣判中的法律法规提取类要基于该类处理
    /// </summary>
    public class QuotLawBase
    {
        /// <summary>
        /// 上下文
        /// </summary>
        public DataContext dataContext { get; set; }

        //提取法律法规
        public LawModelList LawModelList = new LawModelList();
        //合并法律法规
        private LawModelList LawModeMergelList = new LawModelList();

        private XmlUtilCommon xmlUtilCommon = new XmlUtilCommon();

        /// <summary>
        /// 合并法律法规节点
        /// 1.法律数据进行xml转化并存储到上下文
        /// 2.合并法律法规
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="lawmergpath"></param>
        public void MergerLawNodes(string sourcePath,string lawmergpath)
        {
            //计算出法律数据进行转换XML存储
            if (LawModelList.lawModels.Count > 0)
            {
                string xmllawstr = AnalysisModelLaw(LawModelList);
                XmlDocument xmlDocumentlaw = new XmlDocument();
                xmlDocumentlaw.LoadXml(xmllawstr);
                XmlNodeList xmlNodeList = xmlDocumentlaw.SelectNodes("Root/*");
                foreach (XmlNode xmlNode in xmlNodeList)
                {
                    XmlDocument xmlDocumentlawlist = new XmlDocument();
                    xmlDocumentlawlist.LoadXml(xmlNode.OuterXml);
                    DataSoursModel dataSours = xmlUtilCommon.GetXMLByDataModel(xmlDocumentlawlist);
                    DataModel dataModelprent = dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList.FirstOrDefault(a => a.XmlPath == sourcePath);
                    if (dataModelprent == null)
                    {
                        xmlUtilCommon.CaseNodeUpdate(dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList, sourcePath);
                        dataModelprent = dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList.FirstOrDefault(a => a.XmlPath == sourcePath);
                    }
                    foreach (DataModel modeldata in dataSours.dataModelList)
                    {
                        modeldata.XmlPath = sourcePath + "/" + modeldata.XmlPath;
                    }
                    List<DataModel> dataModelListLaw = dataSours.dataModelList;
                    dataModelListLaw.FirstOrDefault(a => a.ParentId == "").ParentId = dataModelprent.ID;
                    dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList.AddRange(dataModelListLaw);
                    XmlDocument xmlDocumentop = XmlUtility.DataModelByXml(dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList);
                    LawModeMergelList.lawModels.AddRange(LawModelList.lawModels);
                }
            }

            #region 合并法律规则
            if (LawModeMergelList.lawModels.Count > 0 && !string.IsNullOrEmpty(lawmergpath))
            {
                LawModelList modelLaw = new LawModelList();
                modelLaw = LawModeMergelList;
                string xmllawstr = AnalysisModelLaw(modelLaw);
                XmlDocument xmlDocumentlaw = new XmlDocument();
                xmlDocumentlaw.LoadXml(xmllawstr);
                XmlNodeList xmlNodeList = xmlDocumentlaw.SelectNodes("Root/*");
                foreach (XmlNode xmlNode in xmlNodeList)
                {
                    XmlDocument xmlDocumentlawlist = new XmlDocument();
                    xmlDocumentlawlist.LoadXml(xmlNode.OuterXml);
                    DataSoursModel dataSours = xmlUtilCommon.GetXMLByDataModel(xmlDocumentlawlist);
                    DataModel dataModelprent = dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList.FirstOrDefault(a => a.XmlPath == lawmergpath);
                    if (dataModelprent == null)
                    {
                        xmlUtilCommon.CaseNodeUpdate(dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList, lawmergpath);
                        dataModelprent = dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList.FirstOrDefault(a => a.XmlPath == lawmergpath);
                    }
                    foreach (DataModel modeldata in dataSours.dataModelList)
                    {
                        modeldata.XmlPath = lawmergpath + "/" + modeldata.XmlPath;
                    }
                    List<DataModel> dataModelListLaw = dataSours.dataModelList;
                    dataModelListLaw.FirstOrDefault(a => a.ParentId == "").ParentId = dataModelprent.ID;
                    dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList.AddRange(dataModelListLaw);
                    //XmlDocument xmlDocumentop = XmlUtility.DataModelByXml(dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList);
                }
            }
            #endregion
        }

        /// <summary>
        /// 解析法律实体为XML结构
        /// </summary>
        /// <param name="modelLaw"></param>
        /// <returns></returns>
        public string AnalysisModelLaw(LawModelList modelLaw)
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlNode xmlNodeRoot = xmlDocument.CreateElement("Root");
            if (modelLaw.lawModels.Count > 0)
            {
                XmlNode xmlNodeLaw = xmlDocument.CreateElement("引用法规");
                foreach (LawModel lawModel in modelLaw.lawModels)
                {
                    XmlNode xmlNodeLawModle = xmlDocument.CreateElement("详情");
                    if (!string.IsNullOrEmpty(lawModel.LawID))
                    {
                        XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用法规ID");
                        xmlAttribute.Value = lawModel.LawID;
                        xmlNodeLawModle.Attributes.Append(xmlAttribute);
                    }
                    if (!string.IsNullOrEmpty(lawModel.LawName))
                    {
                        XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用法规名称");
                        xmlAttribute.Value = lawModel.LawName;
                        xmlNodeLawModle.Attributes.Append(xmlAttribute);

                        XmlAttribute xmlAttributes = xmlDocument.CreateAttribute("法规来源");
                        xmlAttributes.Value = "legation";
                        xmlNodeLawModle.Attributes.Append(xmlAttributes);
                    }
                    if (lawModel.lawProvList.Count > 0)
                    {
                        XmlNode xmlNodeProv = xmlDocument.CreateElement("引用条");
                        foreach (LawProv lawProv in lawModel.lawProvList)
                        {
                            XmlNode xmlNodeProvcontxt = xmlDocument.CreateElement("详情");
                            if (!string.IsNullOrEmpty(lawProv.ProvID))
                            {
                                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用条ID");
                                xmlAttribute.Value = lawProv.ProvID;
                                xmlNodeProvcontxt.Attributes.Append(xmlAttribute);
                            }
                            if (!string.IsNullOrEmpty(lawProv.ProvName))
                            {
                                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用条名称");
                                xmlAttribute.Value = lawProv.ProvName;
                                xmlNodeProvcontxt.Attributes.Append(xmlAttribute);
                            }
                            if (lawProv.lawClauseList.Count > 0)
                            {
                                XmlNode xmlNodeClause = xmlDocument.CreateElement("引用款");
                                foreach (LawClause lawClause in lawProv.lawClauseList)
                                {
                                    XmlNode xmlNodeClauseTxt = xmlDocument.CreateElement("详情");
                                    if (!string.IsNullOrEmpty(lawClause.ClauseID))
                                    {
                                        XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("款排序");
                                        xmlAttribute.Value = lawClause.ClauseID;
                                        xmlNodeClauseTxt.Attributes.Append(xmlAttribute);
                                    }
                                    if (!string.IsNullOrEmpty(lawClause.ClauseName))
                                    {
                                        XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用款名称");
                                        xmlAttribute.Value = lawClause.ClauseName;
                                        xmlNodeClauseTxt.Attributes.Append(xmlAttribute);
                                    }
                                    if (lawClause.lawItemList.Count > 0)
                                    {
                                        XmlNode xmlNodeItem = xmlDocument.CreateElement("引用项");
                                        foreach (LawItem lawItem in lawClause.lawItemList)
                                        {
                                            XmlNode xmlNodeItemTxt = xmlDocument.CreateElement("详情");
                                            if (!string.IsNullOrEmpty(lawItem.ItemID))
                                            {
                                                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("项排序");
                                                xmlAttribute.Value = lawItem.ItemID;
                                                xmlNodeItemTxt.Attributes.Append(xmlAttribute);
                                            }
                                            if (!string.IsNullOrEmpty(lawItem.ItemName))
                                            {
                                                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用项名称");
                                                xmlAttribute.Value = lawItem.ItemName;
                                                xmlNodeItemTxt.Attributes.Append(xmlAttribute);
                                            }
                                            xmlNodeItem.AppendChild(xmlNodeItemTxt);
                                        }
                                        xmlNodeClauseTxt.AppendChild(xmlNodeItem);
                                        ;
                                    }
                                    xmlNodeClause.AppendChild(xmlNodeClauseTxt);
                                }
                                xmlNodeProvcontxt.AppendChild(xmlNodeClause);
                            }
                            xmlNodeProv.AppendChild(xmlNodeProvcontxt);
                        }
                        xmlNodeLawModle.AppendChild(xmlNodeProv);
                    }
                    xmlNodeLaw.AppendChild(xmlNodeLawModle);
                }
                xmlNodeRoot.AppendChild(xmlNodeLaw);
                xmlDocument.AppendChild(xmlNodeRoot);
            }
            return xmlDocument.OuterXml;
        }

        /// <summary>
        /// 添加法律实体
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public LawModel AddLaw(string id, string name)
        {
            LawModel lawModelLaw = LawModelList.lawModels.FirstOrDefault(a => a.LawID == id);
            if (lawModelLaw == null && !string.IsNullOrEmpty(id))
            {
                LawModel lawModel = new LawModel();
                lawModel.LawID = id;
                lawModel.LawName = name;
                lawModelLaw = lawModel;
                LawModelList.lawModels.Add(lawModelLaw);
            }
            return lawModelLaw;
        }

        /// <summary>
        /// 添加法条
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="lawModelLaw"></param>
        /// <returns></returns>
        public LawProv AddLawPro(string id, string name, LawModel lawModelLaw)
        {
            LawProv lawProvLaw = lawModelLaw.lawProvList.FirstOrDefault(a => a.ProvID == id);
            if (lawProvLaw == null && !string.IsNullOrEmpty(id))
            {
                LawProv lawProv = new LawProv();
                lawProv.ProvID = id;
                lawProv.ProvName = name;
                lawModelLaw.lawProvList.Add(lawProv);
                lawProvLaw = lawProv;
            }
            return lawProvLaw;
        }

        /// <summary>
        /// 法款添加
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="lawProvLaw"></param>
        /// <returns></returns>
        public LawClause AddLawClause(string id, string name, LawProv lawProvLaw)
        {
            LawClause lawClauseLaw = lawProvLaw.lawClauseList.FirstOrDefault(a => a.ClauseID == id);
            if (lawClauseLaw == null && !string.IsNullOrEmpty(id))
            {
                LawClause lawClause = new LawClause();
                lawClause.ClauseID = id;
                lawClause.ClauseName = name;
                lawProvLaw.lawClauseList.Add(lawClause);
                lawClauseLaw = lawClause;
            }
            return lawClauseLaw;
        }

        /// <summary>
        /// 法项添加
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="lawClauseLaw"></param>
        /// <returns></returns>
        public LawItem AddLawItem(string id, string name, LawClause lawClauseLaw)
        {
            if (lawClauseLaw == null)
            {
                return null;
            }
            LawItem lawItemLaw = lawClauseLaw.lawItemList.FirstOrDefault(a => a.ItemID == id);
            if (lawItemLaw == null && !string.IsNullOrEmpty(id))
            {
                LawItem lawItem = new LawItem();
                lawItem.ItemID = id;
                lawItem.ItemName = name;
                lawClauseLaw.lawItemList.Add(lawItem);
                lawItemLaw = lawItem;
            }
            return lawItemLaw;
        }

    }
}
